/*
 * 分页请求工具，需要结合宏峰的后台使用；
 * version : "1.0",
 * auther : "陈 庭",
 *
 * Date: 2017-05-22
 *
 */

function HFPager() {

	this.url = '';	//访问URL

	this.limit = 0;	//每页显示行数
	this.offset = 0;	//起始行数
	this.page = 0;	//当前页数
	this.sort = '';	//排序字段条件
	this.order = '';	//排序条件
	this.total = 0;	//总行数
	this.params = {};	//搜索条件
	this.result = [];	//搜索结果

	this.proccessor = function(res){};	//处理方法


	this.init = function(url, limit, page, sort, order){
		this.url = url;

		this.limit = limit;
		this.page = page;
		this.sort = sort;
		this.order = order;

		// this.page = parseInt(this.offset) / parseInt(this.limit) + 1;
		this.offset = (parseInt(this.page) - 1) * parseInt(this.limit);	//偏移值

		this.query(this.page);
	}

	this.query = function(page){

		//计算出偏移值
		this.offset = (parseInt(this.page) - 1) * parseInt(this.limit);	//偏移值
		this.params.limit = this.limit;
		this.params.offset = this.offset;
		this.params.page = this.page;
		this.params.sort = this.sort;
		this.params.order = this.order;

		$.ajax({
		   url: this.url,
		   async: true,	//异步请求
		   type: 'POST',
		   data: this.params,
		   complete:function(XMLHttpRequest, textStatus){
			//alert(XMLHttpRequest.responseText); 
		 },
		   success: function(res){
			   if(res.state == 0){
				   this.total = res.result.total;	//总行数
				   this.result = res.result.list;	//查询的结果
				   
				   this.proccessor(this.result);	//处理查询的结果
			   }
			}
		});
	}
}